﻿using DevExpress.Data.Utils;
using HalconDotNet;
using ImageDefine;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ImageToolCircle
{
    public partial class FormSetting : DevExpress.XtraEditors.XtraForm
    {
        ImageTask imageTask;
        ImageToolCircle imageTool;
        ImageActionerCircle imageActioner;
        public FormSetting()
        {
            InitializeComponent();
        }
        public FormSetting(ImageTask imageTaskIn, ImageToolCircle imageToolIn, ImageActionerCircle imageActionerIn)
        {
            InitializeComponent();
            imageTask = imageTaskIn;
            imageTool = imageToolIn;
            imageActioner = imageActionerIn;
        }

        private void FormSetting_Load(object sender, EventArgs e)
        {
            propertyGridControl1.SelectedObject = imageTool.Setting;
            imageTask.LoadRegeditFile(smartDisplayControl1);

            comboBoxEditTraceName.Text = imageTool.strTraceName;
            foreach (ImageToolBase itemTool in imageTask.listTools)
            {
                if (itemTool is ImageDefine.IAffine)
                    comboBoxEditTraceName.Properties.Items.Add(itemTool.Name);
            }
        }

        private void comboBoxEditTraceName_SelectedValueChanged(object sender, EventArgs e)
        {
            imageTool.strTraceName = comboBoxEditTraceName.EditValue.ToString();
        }

        private void SmartDisplayControl1_drawCircleArcFinished(double Row, double Col, double R, double startAngle, double endAngle)
        {
            //throw new NotImplementedException();
            smartDisplayControl1.drawCircleArcFinished -= SmartDisplayControl1_drawCircleArcFinished; ;
            imageTool.Setting.RowCenter=Row;
            imageTool.Setting.ColumnCenter=Col;
            imageTool.Setting.Radius=R;
            imageTool.Setting.StartAngle=startAngle;
            imageTool.Setting.EndAngle=endAngle;
            //HXLDCont hXLDCont = new HXLDCont();
            //hXLDCont.GenCircleContourXld(Row, Col, R, startAngle, endAngle,1, 1 );
            smartDisplayControl1.DispCircle(Row, Col, R);
            propertyGridControl1.Refresh();
        }

        private void simpleButton画搜索区域_Click(object sender, EventArgs e)
        {
            if (smartDisplayControl1.ho_Image == null)
                return;
            if (imageTool.Setting.RowCenter == 0)
            {
                smartDisplayControl1.CreateCircle();
            }
            else
            {
                smartDisplayControl1.CreateCircle((int)imageTool.Setting.RowCenter, (int)imageTool.Setting.ColumnCenter, imageTool.Setting.Radius);
            }
            // smartDisplayControl1.drawCircleArcFinished += SmartDisplayControl1_drawCircleArcFinished; ;
            smartDisplayControl1.drawCircleFinished += SmartDisplayControl1_drawCircleFinished;
        }

        private void SmartDisplayControl1_drawCircleFinished(double Row, double Col, double R)
        {
            smartDisplayControl1.drawCircleArcFinished -= SmartDisplayControl1_drawCircleArcFinished; ;
            imageTool.Setting.RowCenter = Row;
            imageTool.Setting.ColumnCenter = Col;
            imageTool.Setting.Radius = R;
            imageTool.Setting.StartAngle = 0;
            imageTool.Setting.EndAngle = 2*Math.PI;
            imageTool.Setting.PointOrderCW = true;
            //HXLDCont hXLDCont = new HXLDCont();
            //hXLDCont.GenCircleContourXld(Row, Col, R, startAngle, endAngle,1, 1 );
            smartDisplayControl1.DispCircle(Row, Col, R);
            propertyGridControl1.Refresh();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);

            imageTool.imageTask.RunFileImage( smartDisplayControl1, smartDisplayControl1.ho_Image);

            imageTool.Run(smartDisplayControl1.ho_Image, smartDisplayControl1);
        }

        private void simpleButtonLoadImage_Click(object sender, EventArgs e)
        {
            imageTool.imageTask.LoadRegeditFile(smartDisplayControl1);
        }
    }
}
